//
//  ViewDetailController.m
//  Channel
//
//  Created by CPX on 2018/9/5.
//  Copyright © 2018年 CPX. All rights reserved.
//

#import "VideoDetailController.h"
#import "ChannelVideoModel.h"
#import "ChannelVideoCell.h"
#import "VideoPlayController.h"

@interface VideoDetailController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView *table;
@property (nonatomic, strong) NSMutableArray *list;
@property (nonatomic, strong) UIImageView *emptyView;
@property (nonatomic, strong) MJRefreshAutoNormalFooter *footer;

@end

@implementation VideoDetailController

- (void)viewDidLoad {
    [super viewDidLoad];
    
}

- (void)initViews{
    
    self.view.backgroundColor = UIColorHex(#F6F6F6);
    
    CGFloat margin = 12;
    
    self.table = [[UITableView alloc] initWithFrame:CGRectMake(0, NavHeight + margin, kScreenWidth, kScreenHeight - NavHeight - margin - TabbarHeight) style:UITableViewStylePlain];
    self.table.backgroundColor = UIColorHex(#F6F6F6);
    self.table.delegate = self;
    self.table.estimatedRowHeight = 0;
    self.table.estimatedSectionFooterHeight = 0;
    self.table.estimatedSectionHeaderHeight = 0;
    self.table.dataSource = self;
    CGFloat y = (kScreenWidth-12-12)/312*175;
    self.table.rowHeight = 12+12+y+48;
    self.table.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
//    self.table.mj_footer = self.footer;
    [self.view addSubview:self.table];
    
    
    MJRefreshNormalHeader *he = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(reloadData)];
    he.lastUpdatedTimeLabel.hidden = YES;
    he.stateLabel.hidden = YES;
    self.table.mj_header = he;
//    self.table.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(reloadData)];
    
    self.emptyView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"no_content_img"]];
    self.emptyView.hidden= YES;
    [self.view addSubview:self.emptyView];
    self.emptyView.center = CGPointMake(self.view.width/2, self.view.height/2);
}

- (void)loadData{
    
    self.list = [NSMutableArray array];
    [self.table.mj_header beginRefreshing];
    
}

- (void)reloadData{
    [ChannelApi getChannelListWithName:self.isRecommend?@"recommend":@"popular" offset:0 success:^(id response) {
        
        NSArray *a = [NSArray yy_modelArrayWithClass:[ChannelVideoModel class] json:response];
        [self.list removeAllObjects];
        [self.list addObjectsFromArray:a];
        if (!self.list.count) {
            self.table.mj_footer = nil;
            self.emptyView.hidden = NO;
        }
        else{
            self.table.mj_footer = self.footer;
            self.emptyView.hidden = YES;
        }
        [self.table reloadData];
        [self.table.mj_header endRefreshing];
    } failure:^(NSError *error) {
        if (!self.list.count) {
            self.table.mj_footer = nil;
        }
        [self.table.mj_header endRefreshing];
        [MBProgressTool showTextForView:self.view text:error.userInfo[@"msg"]];
    }];
}

- (void)loadMoreData{
    [ChannelApi getChannelListWithName:self.isRecommend?@"recommend":@"popular" offset:self.list.count success:^(id response) {
        NSArray *a = [NSArray yy_modelArrayWithClass:[ChannelVideoModel class] json:response];
        if (a.count == 10) {
            [self.table.mj_footer endRefreshing];
        }
        else{
            [self.table.mj_footer endRefreshingWithNoMoreData];
        }
        [self.list addObjectsFromArray:a];
        if (!self.list.count) {
            self.table.mj_footer = nil;
        }
        else{
            self.table.mj_footer = self.footer;
        }
        [self.table reloadData];
    } failure:^(NSError *error) {
        [self.table.mj_header endRefreshing];
        [self.table.mj_footer endRefreshing];
        [MBProgressTool showTextForView:self.view text:error.userInfo[@"msg"]];
    }];
}

#pragma mark - table

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.list.count;
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ChannelVideoCell *cell = [ChannelVideoCell cellWithTable:tableView];
    cell.model = self.list[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    VideoPlayController *ctr = [VideoPlayController new];
    ctr.hidesBottomBarWhenPushed = YES;
    ChannelVideoModel *m = self.list[indexPath.row];
    [self logEvent:@"a_click_playvideo" params:@{@"videoid":@(m._id),@"userid":@""}];
    ctr.url = m.video_url;
    [self.navigationController pushViewController:ctr animated:YES];
}

@end
